package com.margo.project.dubbo;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;

public class ZookeeperRegister {
	public static void main(String[] args) throws IOException {
		//int port = 8091;
		int port=2181;
		@SuppressWarnings("resource")
		ServerSocket server = new ServerSocket(port);
		while (true) {
	         //server尝试接收其他Socket的连接请求，server的accept方法是阻塞式的
	         Socket socket = server.accept();
	         //每接收到一个Socket就建立一个新的线程来处理它
	         new Thread(new Runnable() {
				
				@Override
				public void run() {
					InputStreamReader is=null;
					try {
						is= new InputStreamReader(socket.getInputStream(), "UTF-8");
						char[] c=new char[1024];
						
						int index;
						while ((index = is.read(c)) != -1) {
							System.out.println(new String(c, 0, index));
							//阻塞了
							break;
						}
						socket.close();
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					} finally {
					}
					
				}
			}).start();
	    }

	}
}
